{{define "login/modify.html"}}
{{template "header" .}}
{{template "sha1" .}}

<title>修改密码</title>
<script>
    function dlgModifyPassword() {
        var pwd = $('#pwd').val();
        var newpwd = $('#newpwd1').val();
        var pwd2 = $('#newpwd2').val();
        if (newpwd != pwd2) {
			layer.msg('两次新密码不一致')
            return;
        }

        var hash = sha1.create();
        hash.update(pwd);
        pwd = hash.hex();
        $('#pwd').val(pwd)

        hash = sha1.create();
        hash.update(newpwd);
        newpwd = hash.hex();
        $('#newpwd1').val(newpwd);
        $('#newpwd2').val(newpwd);
        //debugger;

        $.ajax({
            type: "POST", url: "/v1/login/modify",
            data: { pwd: pwd, newpwd: newpwd, },
            success: function (data) {
                if (data['msg'] == 'OK') {
                    layer.msg('修改密码成功');
                } else {
					layer.msg( data['msg']);
                }
            },
            error: function (msg) {
				layer.msg("网络错误");
            }
        })
        return false;
    }

</script>

<title>登录</title>
<style>
	html,
	body {
		width: 100%;
		height: 100%;
		margin: 0;
		padding: 0;
	}

	.content {
		width: 350px;
		height: 220px;
		/*background: orange;*/
		margin: 0 auto;
		/*水平居中*/
		position: relative;
		top: 50%;
		/*偏移*/
		transform: translateY(-50%);
	}
</style>
</head>

<body>
	<div class="content">
		<br />
		<div style="text-align: center; margin-bottom: 10px;">
			<h2>修改密码</h2>
		</div>
		<form class="layui-form" action="">
			<div class="layui-form-item">
				<label class="layui-form-label">原&nbsp;密&nbsp;码:</label>
				<div class="layui-input-inline">
					<input id="pwd" type="password" required lay-verify="required" placeholder="请输入原密码" autocomplete="off"
						class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">新&nbsp;密&nbsp;码:</label>
				<div class="layui-input-inline">
					<input type="password" id="newpwd1" type="password" required lay-verify="required" placeholder="请输入新密码"
						autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">再次输入:</label>
				<div class="layui-input-inline">
					<input type="password"  id="newpwd2" type="password" required lay-verify="required" placeholder="请再次输入新密码"
						autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn layui-btn-primary"  onclick="dlgModifyPassword();" >修改密码</button>
				</div>
			</div>
		</form>
	</div>

	<script>
		var form = layui.form;
		//监听提交
		form.on('submit(formDemo)', function (data) {
			try {
				data = data.field;
				if (data.username == "") {
					layer.msg("账号不能为空");
					return false;
				}
				var hash = sha1.create();
				hash.update(data.password);
				var pwd = hash.hex();
				$('#password').val(pwd);
				data.password = pwd;
				console.log(JSON.stringify(data));
				$.ajax({
					type: "POST", url: "/v1/login/index",
					data: data,
					success: function (data) {
						if (data == null || data.msg == null || data.code == null) {
							layer.msg("服务器应答错误")
							return
						}
						if(data.code != 200){
							layer.msg(data.msg);
							return false
						}
						window.location.href = "/";
					},
					error: function (msg) {
						layer.msg("网络错误")
					}
				})
			} catch (err) {
				layer.msg("操作错误:" + err);
				return false;
			}

			return false;
		});

	</script>
</body>

</html>
{{end}}